package com.polaris.live.dialog_fragment.magiclamp

import android.os.Build
import android.os.Bundle
import android.view.View
import com.bumptech.glide.Glide
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.constant.PagConst
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbFragment
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.databinding.FragmentMagicLampBinding
import com.polaris.live.popup.MagicLampRewardPopup
import com.polaris.live.popup.PreviewRichCharmPopup
import com.polaris.live.resp.back_resp.PayerLampItem
import com.polaris.live.resp.back_resp.UserRichCharmLevelRewardExplainVo
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.image.avatar

/**
 * MagicLampFragment 神灯
 *
 * @author Created by 天晴 on 2024/11/22 14:42
 * @since 1.0.0
 **/
class MagicLampFragment : BaseVbFragment<BaseViewModel, FragmentMagicLampBinding>() {

    private val mPayerLampItem by lazy {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            arguments?.getParcelable("data", PayerLampItem::class.java)
        } else {
            arguments?.getParcelable("data")
        }
    }
    private val mUserId by lazy {
        arguments?.getLong("userId", 0)
    }
    private val isLive by lazy {
        arguments?.getBoolean("isLive", false) ?: false
    }
    private var toGift: (() -> Unit)? = null
    override fun initView(savedInstanceState: Bundle?) {
        mPayerLampItem?.let {
            setData(it)
        }
        initListener()
    }

    private fun initListener() {
        mBinding.btnImgWin.setOnSingleClickListener {
            mPayerLampItem?.let {
                MagicLampRewardPopup.show(requireContext(), it.rewards)
            }

        }
        mBinding.pagGift.setOnSingleClickListener {
            toGift?.invoke()
        }
    }

    private fun setData(payerLampItem: PayerLampItem) {
        if (payerLampItem.status == 0L || payerLampItem.status == 1L) {

            mBinding.rewardCl.visibility = View.VISIBLE
            mBinding.contributeCl.visibility = View.GONE

            mBinding.progress.setMax(payerLampItem.total)
            mBinding.progress.setProgress(payerLampItem.progress)
            if (mUserId == UserManager.getUserId() && isLive) {
                mBinding.pagGift.visibility = View.GONE
            } else {
                mBinding.pagGift.visibility = View.VISIBLE
                mBinding.pagGift.apply {
                    path = PagConst.MAGIC_LAMP_GIFT
                    setRepeatCount(-1)
                    play()
                }
            }
            val rewardCls = listOf(
                mBinding.rewardCl1, mBinding.rewardCl2, mBinding.rewardCl3,
                mBinding.rewardCl4, mBinding.rewardCl5
            )
            val rewardImages = listOf(
                mBinding.rewardImage1, mBinding.rewardImage2, mBinding.rewardImage3,
                mBinding.rewardImage4, mBinding.rewardImage5
            )
            val rewardTips = listOf(
                mBinding.rewardTips1, mBinding.rewardTips2, mBinding.rewardTips3,
                mBinding.rewardTips4, mBinding.rewardTips5
            )

            val rewardNames = listOf(
                mBinding.rewardName1, mBinding.rewardName2, mBinding.rewardName3,
                mBinding.rewardName4, mBinding.rewardName5
            )
            payerLampItem.rewards.forEachIndexed { index, payerLampReward ->
                if (index in rewardImages.indices) {
                    Glide.with(requireContext())
                        .load(ImageUtils.imgPath(payerLampReward.displayIcon))
                        .into(rewardImages[index])

                    rewardTips[index].text = payerLampReward.text
                    rewardNames[index].text = payerLampReward.displayText
                }

                rewardCls[index].setOnSingleClickListener {
                    if (payerLampReward.decorationType != null) {
                        var effectPhotoUrl = when (payerLampReward.decorationType) {
                            CommonConst.DressUp.LIVE_EFFECT -> {
                                payerLampReward.effectDisplayUrl ?: ""
                            }

                            else -> {
                                payerLampReward.ext ?: ""
                            }
                        }

                        PreviewRichCharmPopup.show(
                            requireContext(),
                            UserRichCharmLevelRewardExplainVo(
                                "",
                                payerLampReward.displayIcon,
                                effectPhotoUrl,
                                "{\"width\":${160.autoDp},\"height\":${160.autoDp}}",
                                payerLampReward.decorationType,
                                payerLampReward.displayIcon ?: "",
                                1,
                                ""
                            ),
                            false
                        )
                    }

                }
            }

        } else {
            mBinding.rewardCl.visibility = View.GONE
            mBinding.contributeCl.visibility = View.VISIBLE
            val avatarViews = listOf(mBinding.imgAvatar1, mBinding.imgAvatar2, mBinding.imgAvatar3)
            val nameViews = listOf(mBinding.tvName1, mBinding.tvName2, mBinding.tvName3)
            payerLampItem.contributions.forEachIndexed { index, payerLampContribution ->
                if (index in avatarViews.indices) {
                    Glide.with(requireContext())
                        .load(ImageUtils.imgPath(payerLampContribution.userBasic.avatar))
                        .circleCrop()
                        .avatar()
                        .into(avatarViews[index])
                }
                nameViews[index].text = payerLampContribution.userBasic.nickname
            }
        }
    }

    fun setListener(toGift: (() -> Unit)?) {
        this.toGift = toGift
    }
}